package com.ruijie.acl.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.faces.model.SelectItem;

import com.ruijie.acl.AclInterfaceView;
import com.ruijie.emp.device.DeviceInterface;

public class InterfaceSortUtil {
	public final static int MAX_IF_NAME_LENGTH = 25;

	/**
	 * 对接口列表进行排序，根据接口名称
	 * 
	 * @param interfaceList
	 * @return
	 */
	public static List<AclInterfaceView> sortInterfaceItemList(
			List<AclInterfaceView> interfaceList) {
		if (interfaceList != null && !interfaceList.isEmpty()) {
			final Comparator<AclInterfaceView> CSI_BY_IF_NAME_ASC = new Comparator<AclInterfaceView>() {
				public int compare(AclInterfaceView iv1, AclInterfaceView iv2) {
					int r = compareTwo(iv1.getName(), iv2.getName());
					return r;
				}
			};

			Collections.sort(interfaceList, CSI_BY_IF_NAME_ASC);
		} 
		return interfaceList;
	}

	/**
	 * 对接口列表进行排序，根据接口名称
	 * 
	 * @param interfaceList
	 * @return
	 */
	public static List<DeviceInterface> sortInterfaceList(
			List<DeviceInterface> interfaceList) {
		if (interfaceList != null && !interfaceList.isEmpty()) {
			final Comparator<DeviceInterface> CSI_BY_IF_NAME_ASC = new Comparator<DeviceInterface>() {
				public int compare(DeviceInterface iv1, DeviceInterface iv2) {
					int r = compareTwo(iv1.getName(), iv2.getName());
					return r;
				}
			};

			Collections.sort(interfaceList, CSI_BY_IF_NAME_ASC);
		}
		return interfaceList;
	}

	/**
	 * 对接口列表进行排序，根据接口名称
	 * 
	 * @param interfaceList
	 * @return
	 */
	public static List<SelectItem> sortInterfaceItemListSelect(
			List<AclInterfaceView> interfaceList) {
		List<SelectItem> siList = new ArrayList<SelectItem>();
		if (interfaceList != null && !interfaceList.isEmpty()) {
			siList = new ArrayList<SelectItem>(interfaceList.size());
			for (AclInterfaceView _if : interfaceList) {
				siList.add(new SelectItem(_if, _if.getName() + "("
						+ _if.getIfDescr() + ")"));
			}
		}
		return sortInterfaceItemListByItem(siList);
	}

	/**
	 * 对接口列表进行排序，根据接口名称
	 * 
	 * @param interfaceList
	 * @return
	 */
	public static List<SelectItem> sortInterfaceItemListByItem(
			List<SelectItem> interfaceList) {
		// List<SelectItem> siList = new ArrayList<SelectItem>();
		if (interfaceList != null && !interfaceList.isEmpty()) {
			// siList = new ArrayList<SelectItem>(interfaceList.size()) ;
			// for (AclInterfaceView _if : interfaceList) {
			// siList.add(new SelectItem(_if.getIfIndex(), _if.getName() + "(" +
			// _if.getIfDescr()+ ")")) ;
			// }

			if (!interfaceList.isEmpty()) {
				final Comparator<SelectItem> CSI_BY_IF_NAME_ASC = new Comparator<SelectItem>() {
					public int compare(SelectItem si1, SelectItem si2) {
						String siLabel1 = si1.getLabel();
						int pos = siLabel1.indexOf("(");
						if (pos > 0) {
							siLabel1 = siLabel1.substring(0, pos);
						}

						String siLabel2 = si2.getLabel();
						pos = siLabel2.indexOf("(");
						if (pos > 0) {
							siLabel2 = siLabel2.substring(0, pos);
						}
						int r = compareTwo(siLabel1, siLabel2);
						return r;
					}
				};

				Collections.sort(interfaceList, CSI_BY_IF_NAME_ASC);
			}
		}
		return interfaceList;
	}

	/**
	 * 比较两个字符串
	 * 
	 * @param s1
	 * @param s2
	 * @return
	 */
	public static int compareTwo(String s1, String s2) {
		int l1 = s1.length();
		int l2 = s2.length();
		int r = s1.compareTo(s2);

		if (l1 != l2) {
			if (containsDigitOnly(s1) && containsDigitOnly(s2)) {
				if (r > 0) {
					if (l1 < l2) {
						r = -1;
					}
				} else {
					if (l1 > l2) {
						r = 1;
					}
				}
			} else {
				if (r != 0) {
					String maxString;
					String minString;
					int longerLTH;
					int shorterLTH = l1;

					boolean vflag;
					boolean lflag = true;

					if (l1 > l2) {
						longerLTH = l1;
						shorterLTH = l2;
						lflag = true;
					} else if (l1 < l2) {
						longerLTH = l2;
						shorterLTH = l1;
						lflag = false;
					}

					if (r > 0) {
						maxString = s1;
						minString = s2;
						vflag = true;
					} else {
						maxString = s2;
						minString = s1;
						vflag = false;
					}

					int p = maxMatchPos(maxString, minString);
					if ((p >= 0) && (p != shorterLTH - 1)) {
						String ss1 = minString.substring(p + 1);
						String ss2 = maxString.substring(p + 1);

						boolean b1 = containsDigitOnly(ss1);
						boolean b2 = containsDigitOnly(ss2);

						if (b1 && b2) {
							if (vflag && !lflag) {
								r = -1;
							} else if (!vflag && lflag) {
								r = 1;
							}
						}
					}
				}
			}
		}

		return r;

	}

	public static boolean containsDigitOnly(String s) {
		for (int i = 0; i < s.length(); i++) {
			if (!Character.isDigit(s.charAt(i))) {
				return false;
			}
		}

		return true;
	}

	public static int maxMatchPos(String maxs, String mins) {
		int p = -1;

		for (int i = 0; i < mins.length(); i++) {
			if (mins.charAt(i) != maxs.charAt(i)) {
				break;
			} else {
				p = i;
			}
		}

		return p;
	}

	public String getInterfaceName(AclInterfaceView _if) {
		if (_if.getName() != null && !_if.getName().equals("")) {
			return _if.getName();
		} else if (_if.getIfIndex() != null) {
			return _if.getIfIndex().toString();
		} else if (_if.getIfAlias() != null && !(_if.getIfAlias()).equals("")) {
			return _if.getIfAlias();
		} else if (_if.getIfDescr() != null && !(_if.getIfDescr()).equals("")) {
			return cutIfTooLong(_if.getIfDescr());
		} else if (_if.getMac() != null && !(_if.getMac()).equals("")) {
			return _if.getMac();
		}

		return _if.getId().toString();
	}

	public String cutIfTooLong(String s) {
		String s1 = s;
		if (s != null) {
			if (s.length() > MAX_IF_NAME_LENGTH) {
				s1 = s.substring(0, MAX_IF_NAME_LENGTH);
			}
		}

		return s1;
	}
}
